﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SQLite;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace PTCScheduler
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class ViewBy_WeekPage : Page, ScheduleReport
    {
        //SQLite connection for the main window page
        private SQLiteConnection conn;

        //SQLite data dapter for main window page
        private SQLiteDataAdapter da;

        //SQLite database connection string
        private String connString = SystemQueries.dbConnString();

        //Date times for first and last dates of range.
        DateTime firstDate;
        DateTime lastDate;

        //Empty construct.
        public ViewBy_WeekPage()
        {
            //Initialize page.
            InitializeComponent();
        }

        private void weekCalendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            //Gather the range of selected dates.
            SelectedDatesCollection selectedrange = weekCalendar.SelectedDates;

            //If the selected dates are greater than one then display the selected dates in texbox.
            if (selectedrange.Count > 1)
            {
                //Gather first and last date. 
                firstDate = selectedrange[0];
                lastDate = selectedrange[selectedrange.Count - 1];

                //Update textbox to display range.
                weekselecteddatesTextbox.Text = ((DateTime)firstDate).ToString("d") + " - " + ((DateTime)lastDate).ToString("d");

                //Connect to the database
                dbConn();

                //Query results and fill datagridview.
                queryResults();
            }

        }

        //Connect and open connection to database.
        public void dbConn()
        {
            //Create connection.
            conn = new SQLiteConnection(connString);

            //Open connection
            conn.Open();
        }

        //Query Results and fill the corresponding datagridview.
        public void queryResults()
        {
            //Retrieve the today's query string from the SystemQueries class. 
            String sql = SystemQueries.weekQueryString(firstDate,lastDate);

            //Create dataset to fill with the results of the query. 
            DataSet ds = new DataSet(); //System.Data assembly

            //User SQLiteDataAdapter to retrieve results. 
            da = new SQLiteDataAdapter(sql, connString);

            //Fill the dataset with the results. 
            da.Fill(ds);

            //If there are no records then prompt user that there are no scheduled
            //conferences for today. Otherwise just display records. 
            if (ds.Tables[0].Rows.Count == 0)
            {
                weeknoconferenceslbl.Visibility = Visibility.Visible;
            }
            else
            {
                weeknoconferenceslbl.Visibility = Visibility.Hidden;
            }

            //Format date and time column.
            dtFormat(ds);

            //Display results in datagrid view. 
            weekdataGrid.ItemsSource = ds.Tables[0].DefaultView;

            //Close database connection.
            conn.Close();
        }

        //Method to format the date and time column
        private void dtFormat(DataSet ds)
        {
            //Iterate through each row and format accordinly
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                //Create date time object
                DateTime dt = DateTime.Parse(row["Time"].ToString());
                //Set column equal to desired date pattern.
                row["Time"] = dt.ToString("hh:mm tt");
            }
        }

        //Handles Export Button Click Event.
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            //Check to ensure data exists in datagrid to export.
            if (weekdataGrid.Items.Count > 0)
            {
                //Create new instance of CSVExport object which invokes the export to CSV method
                //to export data to excel worksheet.
                new CSVExport(weekdataGrid);
            }
        }

        //Handles Print Button Click Event.
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            //Create new instance of the PrintDataGrid object which invokes the print feature
            //that prints contents of datagrid.
            new PrintDataGrid(weekdataGrid);
        }

    }
}
